var express = require('express');
var router = express.Router();

const collection = require("../models/collection")

const formidable = require('formidable');

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            if (fields.cancel == true) {
                collection.remove({
                    attachment: fields.attachment,
                    question_ID: fields.question_ID, // point_id
                    UUID: fields.UUID
                }, (error, doc) => {
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({ 'state': true, 'msg': "ok" });
                        return
                    }
                })
            } else if ((fields.cancel == false)) {
                collection.create({
                    attachment: fields.attachment,
                    question_ID: fields.question_ID, // point_id
                    UUID: fields.UUID
                }, (error, doc) => {
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({ 'state': true, 'msg': "ok" });
                        return
                    }
                })
            }

        }

    });

});

module.exports = router;